package com.cy.aircompressor.main.home.lifecycle.detail;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cy.aircompressor.databinding.DialogMaintenanceDetailFragmentBinding;
import com.cy.aircompressor.entity.maintenance.MaintenanceRecord;
import com.cy.aircompressor.entity.maintenance.MaintenanceRecordDetail;
import com.cy.aircompressor.main.home.lifecycle.provider.MaintenanceRecordDetailProvider;
import com.cy.aircompressor.main.ui.lifecycle.viewmodel.LifeCycleViewModel;
import com.drug.lib.util.DensityUtil;
import com.drug.lib.view.recycptr.DynamicAdpTypePool;
import com.drug.lib.view.recycptr.DynamicRecyclerAdapter;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.Resource;
import com.net.model.base.fragment.BaseVMDialogFragment;

import java.util.concurrent.TimeUnit;

public class MaintenanceDetailDialog extends BaseVMDialogFragment<DialogMaintenanceDetailFragmentBinding, LifeCycleViewModel> {
    private String id;

    public static MaintenanceDetailDialog showDialogFragment(FragmentManager fragmentManager, String tag, String id) {
        Bundle args = new Bundle();
        MaintenanceDetailDialog fragment = new MaintenanceDetailDialog();
        args.putSerializable("id", id);
        fragment.setArguments(args);
        fragment.show(fragmentManager, tag);
        return fragment;
    }


    @Override
    protected DialogMaintenanceDetailFragmentBinding getViewBinding() {
        return DialogMaintenanceDetailFragmentBinding.inflate(getLayoutInflater());
    }

    @Override
    protected int getWindowHeight() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    @Override
    protected int getWindowWidth() {
        return (super.getScreenWidth() /2);
    }

    @Override
    protected void dataChange() {
        mViewModel.getQueryMaintenanceRecordById().observe(this, new Observer<Resource<MaintenanceRecord>>() {
            @Override
            public void onChanged(Resource<MaintenanceRecord> maintenanceRecordResource) {
                maintenanceRecordResource.handler(new OnCallback<MaintenanceRecord>() {
                    @Override
                    public void onSuccess(MaintenanceRecord maintenanceRecord) {
                        mBinding.compressorName.setText("维保机器名称：" + maintenanceRecord.getCompressorName());
                        mBinding.compressorSn.setText("维保机器编号：" + maintenanceRecord.getCompressorSn());
                        mBinding.startDate.setText("维保开始时间：" + maintenanceRecord.getStartDate());
                        mBinding.endDate.setText("维保结束时间：" + maintenanceRecord.getEndDate());
                        mBinding.nextMaintenanceDate.setText("下次维保时间：" + maintenanceRecord.getNextMaintenanceDate());

                        mBinding.maintainer.setText(String.format("%s%s", "操作人：\u3000\u3000\u3000", maintenanceRecord.getMaintainer()));
                        mBinding.remark.setText(String.format("%s%s", "备注：\u3000\u3000\u3000\u3000", (TextUtils.isEmpty(maintenanceRecord.getRemark()) ? "无备注" : maintenanceRecord.getRemark())));
                        mBinding.createTime.setText(String.format("%s%s", "创建时间：\u3000\u3000", maintenanceRecord.getCreateTime()));
                        mBinding.updateTime.setText(String.format("%s%s", "更新时间：\u3000\u3000", maintenanceRecord.getUpdateTime()));

                        if (maintenanceRecord.getSensorDetailList() == null || maintenanceRecord.getSensorDetailList().size() == 0) {
                            mBinding.sensorDetailNull.setVisibility(View.VISIBLE);
                            mBinding.sensorDetail.setVisibility(View.GONE);
                            mBinding.sensorDetailTitle.setVisibility(View.GONE);
                        } else {
                            mBinding.sensorDetailNull.setVisibility(View.GONE);
                            mBinding.sensorDetail.setVisibility(View.VISIBLE);
                            mBinding.sensorDetailTitle.setVisibility(View.VISIBLE);

                            SensorDetailRecyclerAdapter.setItems(maintenanceRecord.getSensorDetailList());
                            SensorDetailRecyclerAdapter.notifyDataSetChanged();
                        }
                        if (maintenanceRecord.getComponentDetailList() == null || maintenanceRecord.getComponentDetailList().size() == 0) {
                            mBinding.componentDetailNull.setVisibility(View.VISIBLE);
                            mBinding.componentDetail.setVisibility(View.GONE);
                            mBinding.componentDetailTitle.setVisibility(View.GONE);
                        } else {
                            mBinding.componentDetailNull.setVisibility(View.GONE);
                            mBinding.componentDetail.setVisibility(View.VISIBLE);
                            mBinding.componentDetailTitle.setVisibility(View.VISIBLE);

                            ComponentDetailRecyclerAdapter.setItems(maintenanceRecord.getComponentDetailList());
                            ComponentDetailRecyclerAdapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {
        id = getArguments().getString("id");
        RxView.clicks(mBinding.closeBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    dismiss();
                });
        initSensorDetailRecyclerAdapter();
        initComponentDetailRecyclerAdapter();


    }

    @Override
    protected void requestNetwork() {
        mViewModel.queryMaintenanceRecordById(id);
    }


    DynamicRecyclerAdapter SensorDetailRecyclerAdapter;

    private void initSensorDetailRecyclerAdapter() {
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.sensorDetail.setLayoutManager(mLayoutManager);
        SensorDetailRecyclerAdapter = new DynamicRecyclerAdapter();
        DynamicAdpTypePool pool = new DynamicAdpTypePool();
        MaintenanceRecordDetailProvider maintenanceRecordDetailProvider = new MaintenanceRecordDetailProvider(mContext);
        pool.register(MaintenanceRecordDetail.class, maintenanceRecordDetailProvider);
        SensorDetailRecyclerAdapter.registerAll(pool);
        mBinding.sensorDetail.setAdapter(SensorDetailRecyclerAdapter);
    }

    DynamicRecyclerAdapter ComponentDetailRecyclerAdapter;

    private void initComponentDetailRecyclerAdapter() {
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.componentDetail.setLayoutManager(mLayoutManager);
        ComponentDetailRecyclerAdapter = new DynamicRecyclerAdapter();
        DynamicAdpTypePool pool = new DynamicAdpTypePool();
        MaintenanceRecordDetailProvider maintenanceRecordDetailProvider = new MaintenanceRecordDetailProvider(mContext);
        pool.register(MaintenanceRecordDetail.class, maintenanceRecordDetailProvider);
        ComponentDetailRecyclerAdapter.registerAll(pool);
        mBinding.componentDetail.setAdapter(ComponentDetailRecyclerAdapter);
    }
}
